package com.orangelife.mobile.property.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.OrangeHttpBiz;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.IntentHelper;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity3;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.property.adapter.PropertyBillHistoryListAdapter;
import com.orangelife.mobile.property.adapter.PropertyBillHouseListAdapter;
import com.orangelife.mobile.property.bean.PropertyBillHistoryListEntity;
import com.orangelife.mobile.property.bean.PropertyFeeListEntity;
import com.orangelife.mobile.property.biz.PropertyHttpBiz;
import com.orangelife.mobile.property.listener.OnOrderHasPaidByOthersListener;
import com.orangelife.mobile.util.BLog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 物业缴费(小区房屋列表)
 * Created by ${shz} on 2015/12/3.Modify by wgf on 160506.
 *
 * @author wgf
 */
public class PropertyBillHouseListActivity extends PullRefreshBaseActivity3 {
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.llRightClick)
    LinearLayout llRightClick;
    @Bind(R.id.pullListView)
    PullToRefreshListView pullListView;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    int pageSize = 100;

    /** 物业缴费列表适配器（房屋列表） */
    private PropertyBillHouseListAdapter mHouseAdapter;
    /** 历史缴费列表适配器 */
    private PropertyBillHistoryListAdapter mHistoryAdapter;
    private String listType = PROPERTY_LIST_TYPE_HOUSE;
    public static final String PROPERTY_LIST_TYPE_HOUSE = "1";
    public static final String PROPERTY_LIST_TYPE_HISTORY = "2";
    private List<PropertyFeeListEntity> mHouseList = new ArrayList<>();
    private List<PropertyBillHistoryListEntity> historyList = new ArrayList<>();

    /** Activit 名称 **/
    final String MODULE_NAME = "/mine/propertyOdr";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_property_house_list);
        ButterKnife.bind(this);
        setListener();
        request(false);
    }

    @Override
    protected boolean isDelayRequest() {
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        BLog.getInstance().intAppmod(BLog.PMFEEORDER);
    }

    /**
     * 数据采集体不要删除
     *
     * @return
     */
    public Map<String, Object> getDataTrack() {
        Map<String, Object> map = new HashMap<>();
        map.put("UI_HOST", DataTrack.APP_NAME);
        map.put("UI_PATH", MODULE_NAME);
        return map;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        request(false);
    }

    private void getIntentInfo() {
        Intent intent = getIntent();
        listType = String.valueOf(intent.getStringExtra("listType"));
    }

    private void initView() {
        if (listType.equals(PROPERTY_LIST_TYPE_HOUSE)) {
            tvTitle.setText(getResources().getString(R.string.i_want_pay_cost));
            ivTitleRight.setVisibility(View.VISIBLE);
            ivTitleRight.setBackgroundResource(R.drawable.ic_new_add);
        } else {
            tvTitle.setText("历史缴费");
            ivTitleRight.setVisibility(View.GONE);
        }
    }

    private void request(boolean isNeedNotInit) {
        getIntentInfo();
        setAdapter();
        initView();
        if (!isNeedNotInit) {
            setPullRefreshBase();
        }
        if (listType.equals(PROPERTY_LIST_TYPE_HOUSE)) {
            pageSize = 100;
            requestHouseFeeList();
        } else {
            pageSize = 5;
            requestFeeHistoryList();
        }
    }

    private void setListener() {
        ivTitleRight.setOnClickListener(listener);
        emptyLayout.setOnLayoutClickListener(listener);
    }

    private void setAdapter() {
        if (listType.equals(PROPERTY_LIST_TYPE_HOUSE)) {
            if (mHouseAdapter == null) {
                mHouseAdapter = new PropertyBillHouseListAdapter(this, mHouseList);
            }
        } else {
            if (mHistoryAdapter == null) {
                mHistoryAdapter = new PropertyBillHistoryListAdapter(this, historyList);
                mHistoryAdapter.setOnOrderHasPaidByOthersListener(new OnOrderHasPaidByOthersListener() {
                    @Override
                    public void onClick() {
                        request(true);
                    }
                });
            }
        }
    }

    private void setPullRefreshBase() {
        if (listType.equals(PROPERTY_LIST_TYPE_HOUSE)) {
            setXListViewAndEmptyLayoutListener(pullListView, emptyLayout, mHouseAdapter);
        } else {
            setXListViewAndEmptyLayoutListener(pullListView, emptyLayout, mHistoryAdapter);
        }
    }

    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        if (listType.equals(PROPERTY_LIST_TYPE_HOUSE)) {
            return mHouseList;
        } else {
            return historyList;
        }
    }

    @Override
    protected int getDataCount() {
        if (listType.equals(PROPERTY_LIST_TYPE_HOUSE)) {
            return mHouseList == null ? 0 : mHouseList.size();
        } else {
            return historyList == null ? 0 : historyList.size();
        }
    }

    @Override
    protected void requestWhenPullRefresh() {
        request(true);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_title_right:
                    jumpToChooseHouse();
                    break;
                case R.id.emptyLayout:
                    request(true);
                    break;
                default:
                    break;
            }
        }
    };

    /** 获取我的物业缴费信息（展示物业缴费界面） */
    private void requestHouseFeeList() {
        PropertyHttpBiz.getFeeList(pageNo, pageSize, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                dismissProgress();
                List<PropertyFeeListEntity> list = PropertyHttpBiz.handleFeeList(response);
                if (list != null && list.size() == 0) {
                    jumpToChooseHouse();
                    finish();
                } else {
                    handleAfterRequest(isConnected(false), OrangeHttpBiz.getTotalNum(response), list, pullListView, mHouseAdapter);
                }
            }
        });
    }

    /** 请求“历史缴费”列表（展示历史缴费界面） */
    private void requestFeeHistoryList() {
        PropertyHttpBiz.getFeeHistoryList(pageNo, pageSize, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                dismissProgress();
                List<PropertyBillHistoryListEntity> list = PropertyHttpBiz.handleFeeHistoryList(response);
                if (list != null && list.size() == 0) {
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                    emptyLayout.setErrorMessage("暂无历史缴费记录");
                    emptyLayout.setErrorImage(R.drawable.property_no_history);
                } else {
                    handleAfterRequest(errCode, isConnected(false), OrangeHttpBiz.getTotalNum(response), list, pullListView, mHistoryAdapter);
                }
            }
        });
    }

    private void jumpToChooseHouse() {
        IntentHelper.getInstances().intentWithExtra(this, ChooseHouseActivity.class, null, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BLog.getInstance().outAppmod(BLog.PMFEEORDER);
    }
}
